package com.shutuo.callnumqixintian.utils;

import android.text.TextUtils;
import android.util.Log;

import com.shutuo.callnumqixintian.QixintianManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class QiXinTianJsParamsUtil {

    /**
     *   {
     "errcode": 0,
     "errmsg": "OK",
     "res": {
     "tableList": [{
     "qtId": "3131",
     "qtName": "中桌",
     "qtNumPrefix": "B",
     "curNumber": "3",
     "queueNum": "0",
     "lastNumber": "3",
     "qtMin": "5",
     "qtMax": "6"
     }, {
     "qtId": "3130",
     "qtName": "小桌",
     "qtNumPrefix": "A",
     "curNumber": "3",
     "queueNum": "0",
     "lastNumber": "3",
     "qtMin": "1",
     "qtMax": "4"
     }],
     "qRules": ["过号重新取号"]
     }
     }
     */

    private static JSONObject parseData(String data) {
        Log.d(QixintianManager.TAG, "parseData: "+data);
        JSONObject jo = new JSONObject();
        try {
            JSONObject dataObject = new JSONObject(data);
            String errmsg = dataObject.optString("errmsg");
            if(errmsg.startsWith("OK")){
                String res = dataObject.optString("res");
                JSONObject tableObject = new JSONObject(res);
                JSONArray jsonArray = tableObject.optJSONArray("tableList");
                if(jsonArray!=null){
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject = jsonArray.optJSONObject(i);
                        int min = Integer.parseInt(jsonObject.optString("qtMin"));
                        int max = Integer.parseInt(jsonObject.optString("qtMax"));
                        String qtNumPrefix = jsonObject.optString("qtNumPrefix");
                        String curNumber = jsonObject.optString("curNumber");
                        if (min<=1){
                            jo.put("smallTable",qtNumPrefix+curNumber);
                        }
                        else if (max<=6 && min>=5){
                            jo.put("inTable",qtNumPrefix+curNumber);
                        }else {
                            jo.put("bigTable",qtNumPrefix+curNumber);
                        }
                    }
                }else {
                    jo =  new JSONObject("{\"bigTable\":0, \"inTable\":0, \"smallTable\":0}");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jo;
    }

    public static String constructQiXinTianJsParams(String data){

        JSONObject maybeParsedJson = parseData(data);
        if (maybeParsedJson == null){
            return null;
        }
        Log.d(QixintianManager.TAG, "[QiXinTianCallNumberPlayListener] constructData: "+maybeParsedJson.toString());
        String big = maybeParsedJson.optString("bigTable");
        String small = maybeParsedJson.optString("smallTable");
        String in = maybeParsedJson.optString("inTable");
        if(TextUtils.isEmpty(big)) big = "0";
        if(TextUtils.isEmpty(small)) small = "0";
        if(TextUtils.isEmpty(in)) in = "0";
        String type = "qixintian";

        return "window.PushDataForFactory(\"{\\\"bigTable\\\":" + "\\\"" + big + "\\\"" + ",\\\"smallTable\\\":" + "\\\"" + small+ "\\\"" +
                ",\\\"factory\\\":" + "\\\"" + type+ "\\\""+
                ",\\\"inTable\\\":" + "\\\"" + in + "\\\"" +  "}\")";
    }

}
